package com.whh.service.user;

import com.whh.util.BaseUtils;
import com.whh.dao.user.UserDao;
import com.whh.dao.user.UserDaoImpl;
import com.whh.pojo.User;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class UserServiceImpl implements UserService{

    //业务层调用dao层
    private UserDao userDao;
    public UserServiceImpl(){

        userDao=new UserDaoImpl();
    }
    //登录
    @Override
    public User login(String userCode) {
        Connection connection=null;
        User user=null;
        try {
            connection= BaseUtils.getConnection();
            //业务层调用对应的数据库操作
            user=userDao.login(connection,userCode);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            BaseUtils.closeResource(connection,null,null);
        }
        return user;
    }
   //修改密码
    @Override
    public boolean updatePwd(int id, String pwd) {
        Connection conn=null;
        boolean flag=false;
        try {
            conn= BaseUtils.getConnection();
            if (userDao.updatePwd(conn,id,pwd)>0){
                  flag=true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return flag;
    }
    //根据用户名或者角色查询用户总数
    @Override
    public int getUserCount(String username, int userRole) {
        Connection connection=null;
        int count=0;
        try {
            connection = BaseUtils.getConnection();
            count = userDao.getUserCount(connection, username, userRole);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            BaseUtils.closeResource(connection,null,null);
        }
        return count;
    }
    //查询用户列表
    @Override
    public List<User> getUserList(String queryUserName, int queryUserRole, int currentPageNo, int pageSize) {
    Connection connection=null;
    List<User> userList=null;
        System.out.println("queryUserName---->"+queryUserName);
        System.out.println("queryUserRole---->"+queryUserRole);
        System.out.println("currentPageNo---->"+currentPageNo);
        System.out.println("pageSize--------->"+pageSize);

        try {
            connection= BaseUtils.getConnection();
            userList=userDao.getUserList(connection,queryUserName,queryUserRole,currentPageNo,pageSize);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            BaseUtils.closeResource(connection,null,null);
        }

        return userList;

    }
    //添加用户
    @Override
    public boolean add(User user) {
        Connection conn=null;
        boolean flag=false;
        try {
            //开启jdbc的事务  设置自动提交
            conn=BaseUtils.getConnection();
            conn.setAutoCommit(false);
            int updateRows =  userDao.add(conn,user);
            conn.commit();

            if (updateRows>0){
                flag=true;
                System.out.println("add success");
            }else {
                System.out.println("add failed");
            }

        } catch (SQLException throwables) {
            System.out.println("rollback....");
            try {
                conn.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            throwables.printStackTrace();
        }finally {
            BaseUtils.closeResource(conn,null,null);
        }
        return flag;
    }
    //根据userCode查询出User
    @Override
    public User selectUserCodeExist(String userCode) {
        Connection connection = null;
        User user = null;
        try {
            connection = BaseUtils.getConnection();
            user = userDao.getLoginUser(connection, userCode);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseUtils.closeResource(connection, null, null);
        }
        return user;
    }
    //修改用户
    @Override
    public boolean modify(User user) {
     Connection connection=null;
     boolean flag=false;
        try {
            System.out.println("serviceImpl-----------> gogogo");
            connection=BaseUtils.getConnection();
            if (userDao.modify(connection,user)>0){
             flag=true;
            }
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }finally {
            BaseUtils.closeResource(connection,null,null);
        }
        return flag;
    }
    //删除用户
    @Override
    public boolean deleteUserById(Integer delId) {
        Connection conn=null;
        boolean flag=false;
        try {
            conn=BaseUtils.getConnection();
            if (userDao.deleteUserById(conn,delId)>0){
            flag=true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            BaseUtils.closeResource(conn,null,null);
        }

        return flag;
    }
    //通过userId获取user
    @Override
    public User getUserById(String id) {
        Connection conn=null;
        User user=null;
        try {
            conn=BaseUtils.getConnection();
            user=userDao.getUserById(conn,id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            user=null;
        }finally {
            BaseUtils.closeResource(conn,null,null);
        }
        return user;
    }
}















